package com.choosefine.it.manager.service.Impl;

import java.util.Map;

import com.choosefine.it.basedata.service.CountService;
import com.choosefine.it.team.dao.TeamFindProjectInfoMapper;
import com.choosefine.it.team.model.TeamFindProjectInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.manager.dao.ManagerFindTeamInfoMapper;
import com.choosefine.it.manager.model.ManagerFindTeamInfo;
import com.choosefine.it.manager.service.ManagerFindTeamInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * @author 潘钱钦
	下午3:02:43
 *
 */
@Service("managerFindTeamInfoService")
public class ManagerFindTeamInfoServiceImpl implements ManagerFindTeamInfoService {
	@Autowired
	private ManagerFindTeamInfoMapper managerFindTeamInfoMapper;
	@Autowired
	MongoTemplate mongoTemplate;
	@Autowired
	TeamFindProjectInfoMapper teamFindProjectInfoMapper;
	@Autowired
	CountService countService;
	/*项目经理根据条件找班组和搜索yuxin3.11*/
	@Override
	public Page<ManagerFindTeamInfo> selectTeamFindProjectList(Map<String, Object> params, PageDTO pageDto) {
		PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
		Page<ManagerFindTeamInfo> page = managerFindTeamInfoMapper.selectTeamFindProjectList(params);
		return page;
	}

	@Override/*yuin3.6查询项目经理找班组详情*/
	public TeamFindProjectInfo selectById(Map map) {
		TeamFindProjectInfo teamFindProjectInfo=mongoTemplate.findOne(new Query(Criteria.where("_id").is(map.get("publishId"))),TeamFindProjectInfo.class);
		Integer count=0;
		if(teamFindProjectInfo!=null){
			 count=countService.isInsert(map);
			teamFindProjectInfo.setIsInsert(count);
		}
		return teamFindProjectInfo;
	}
	/*项目经理收藏班组yuxin3.11*/
	@Override
	public int managerCollectionTeam(Map<String, Object> params) {
		int count=countService.isInsert(params);//查看是否收藏1代表收藏0代表没收藏.
		int num=0;
		if(0==count){
		 num=managerFindTeamInfoMapper.managerCollectionTeam(params);
			Update update=new Update();
			int favoriteCount=countService.favoriteCount(params);//查询这个信息被收藏次数
			update.set("favoriteCount",favoriteCount);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("publishId"))),update,TeamFindProjectInfo.class);
		}
		return num;

	}

/*项目经理取消收藏班组yuxin3.11*/
	@Override
	public int cancelCollectionTeam(Map<String, Object> params) {
		int count=countService.isInsert(params);//查看是否收藏1代表收藏0代表没收藏.
		int num=0;
		if(count>0){
			num=managerFindTeamInfoMapper.cancelCollectionTeam(params);
			Update update=new Update();
			int favoriteCount=countService.favoriteCount(params);//查询这个信息被收藏次数
			update.set("favoriteCount",favoriteCount);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("publishId"))),update,TeamFindProjectInfo.class);
		}
		return num;
	}
/*项目经理收藏班组列表yuxin3.11*/
	@Override
	public Page<ManagerFindTeamInfo> getCollectionList(Map<String, Object> params) {
		PageHelper.startPage((Integer) params.get("pageNum"),(Integer) params.get("pageSize"));
		Page<ManagerFindTeamInfo> page=managerFindTeamInfoMapper.getCollectionList(params);
		return page;
	}

}
